<?php 

class UserModel extends Model{
	public $table='user';
	public function __init()
	{
	}
	//用户注册
	public function zhuceUser()
	{
		if (empty($_POST['username'])) {
			$this->error='用户名不能为空';
			return false;
		}
		if (empty($_POST['password'])) {
			$this->error='密码不能为空';
			return false;
		}
		if (empty($_POST['code'])) {
			$this->error='验证码不能为空';
			return false;
		}
		$user=$this->where(" username='{$_POST['username']}'")->find();
		if (strtoupper($_POST['code'])!=$_SESSION['code']) {
			$this->error='验证码不正确';
			return false;
		}
		if (empty($user)) {
			if ($this->insert($_POST)) {
				$user2=$this->where(" username='{$_POST['username']}'")->find();
				$_SESSION['uid']=$user2['uid'];
				$_SESSION['username']=$user2['username'];
				return true;
			}else{
				$this->error='注册失败';
				return false;
			}
		}else{
			$this->error='用户名已存在';
		}
	}

	//用户登录
	public function loginUser()
	{
		if (empty($_POST['uname'])) {
			$this->error='用户名不能为空';
			return false;
		}
		if (empty($_POST['pwd'])) {
			$this->error='密码不能为空';
			return false;
		}
		$user=$this->where(" username='{$_POST['uname']}'")->find();
		if ($user) {
			if (strtoupper($user['username'])!==strtoupper($_POST['uname'])) {
				$this->error='用户名不正确';
				return false;
			}
			if ($user['password']!==$_POST['pwd']) {
				$this->error='密码不正确';
				return false;
			}	
				$_SESSION['uid']=$user['uid'];
				$_SESSION['username']=$user['username'];
				return true;
		}else{
			$this->error='用户不存在,请注册后登录';
		}
	}

}


 ?>